package com.fau.cm.action.index;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.OutputStream;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.cm.action.PublicAction;
import com.fau.cm.entity.MedicalAdmin;
import com.fau.cm.entity.MedicalCallSms;
import com.fau.cm.util.FileNameEncoderUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc  短信管理Action
 * @author hufq
 * @datetime 2014年10月13日,下午3:41:30
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class SmsAction extends PublicAction implements ModelDriven<MedicalCallSms> {
	
	private static final long serialVersionUID = 4035736334361437650L;
	private MedicalCallSms callSms = new MedicalCallSms();
	
	/**
	 * @desc 初始化列表页面
	 * @return
	 * @author hufq
	 * @datetime 2014年10月13日,下午4:16:48
	 */
	@Action(value = "sms_init", results = {@Result(name = SUCCESS, location = "sms_init.jsp")})
	public String init(){
		return SUCCESS;
	}
	
	/**
	 * @desc 打开短信管理导入页面
	 * @return String
	 * @author liy
	 * @datetime 2015年2月13日,下午4:29:52
	 */
	@Action(value = "sms_import_add", results = {@Result(name = SUCCESS, location = "sms_import_add.jsp")})
	public String importView(){
		return SUCCESS;
	}
	
	/**
	 * @desc 进入短信发送开关设置
	 * @return String
	 * @author liy
	 * @datetime 2015年2月14日,下午2:57:19
	 */
	@Action(value = "sms_switch", results = {@Result(name = SUCCESS, location = "sms_switch.jsp")})
	public String smsSwitch(){
		return SUCCESS;
	}

	/**
	 * @desc  列表显示页面
	 * @return
	 * @author hufq
	 * @datetime 2014年10月14日,下午2:48:38
	 */
	@Action(value = "sms_view")
	public String view(){
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		this.pageBean = this.baseAllService.getSmsService().findSmsByPage(admin, callSms, map);
		this.map.clear();
		this.map.put("total", this.pageBean.getTotalRow());
		this.map.put("data", this.pageBean.getResult());
		formatJSON(response, this.map);
		return null;

	}
	
	/**
	 * @desc 短信信息添加页面
	 * @return
	 * @author hufq
	 * @datetime 2014年10月14日,上午10:36:58
	 */
	@Action(value = "sms_add", results = {@Result(name = SUCCESS, location = "sms_add.jsp")})
	public String add(){
		return SUCCESS;
	}
	
	/**
	 * @desc 保存短信信息
	 * @return null
	 * @author liy
	 * @datetime 2014年10月14日,上午10:37:31
	 */
	@Action(value = "sms_save")
	public String save(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		Boolean flag = this.baseAllService.getSmsService().saveSms(admin, callSms);
		if(flag){
			map.put("success", true);
		}else{
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 删除短信信息
	 * @return
	 * @author hufq
	 * @datetime 2014年10月14日,下午3:56:24
	 */
	@Action(value="sms_del")
	public String del(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(callSms.getId())){
			this.baseAllService.getSmsService().delSms(callSms);
			map.put("success", true);
		}else{
			map.put("success", false);
			}
		formatJSON(response, map);
		return null;
	}

	/**
	 * @desc 编辑短信页面
	 * @return
	 * @author hufq
	 * @datetime 2014年10月14日,下午3:57:41
	 */
	@Action(value = "sms_edit", results = {@Result(name = SUCCESS, location = "sms_edit.jsp")})
	public String edit(){
		return SUCCESS;
	}
	
	/**
	 * @desc 保存编辑短信信息
	 * @return
	 * @author hufq
	 * @datetime 2014年10月14日,下午4:14:07
	 */
	@Action(value = "sms_save_edit")
	public String editSave(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(callSms.getId())){
			this.baseAllService.getSmsService().saveEditCallSms(callSms);
			map.put("success", true);
		}else{
			map.put("success", false);
			}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 查看详细信息页面
	 * @return
	 * @author hufq
	 * @datetime 2014年10月14日,下午7:14:49
	 */
	@Action(value = "sms_view_detail", results = {@Result(name = SUCCESS, location = "sms_view_detail.jsp")})
	public String viewDetail(){
		return SUCCESS;
	}
	
	/**
	 * @desc 导出全部短信信息
	 * @return
	 * @author hufq
	 * @datetime 2014年10月14日,下午9:25:53
	 */
	@Action(value = "export_all")
	public String exportAll(){
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"短信管理导出模板.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			Workbook workbook = this.baseAllService.getSmsService().exportExcelAllSms(admin);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 导出当前短信信息
	 * @return
	 * @author hufq
	 * @datetime 2014年10月15日,上午9:32:33
	 */
	@Action(value = "export_current")
	public String exportCurrent(){
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"短信管理导出模板.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			Workbook workbook = this.baseAllService.getSmsService().exportExcelCurrentSms(admin, callSms.getIds());
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 查询客户短信页面初始化
	 * @return
	 * @author kangyj
	 * @datetime 2014年12月5日,下午2:21:13
	 */
	@Action(value = "customer_sms_init", results = {@Result(name = SUCCESS, location = "customer_sms_init.jsp")})
	public String customerSmsInit() {
		return SUCCESS;
	}
	
	/**
	 * @desc 根据客户id查询客户短信
	 * @return
	 * @author kangyj
	 * @datetime 2014年12月5日,下午2:27:51
	 */
	@Action(value = "customer_sms_view")
	public String customerSmsView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		list = this.baseAllService.getSmsService().findCustomerSmsList(callSms.getCustomerId());
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 查询话务组
	 * @return null
	 * @author liy
	 * @datetime 2014年12月17日,上午10:54:11
	 */
	@Action(value="sms_find_group")
	public String smsFindGroup() {
		HttpServletResponse response = ServletActionContext.getResponse();
		list = this.baseAllService.getSmsService().findAllCurSerGroup(callSms.getCenterId());
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 查询话务中心
	 * @return null
	 * @author liy
	 * @datetime 2014年12月17日,下午1:01:15
	 */
	@Action(value="sms_find_center")
	public String smsFindCenter() {
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		list = this.baseAllService.getSmsService().findAllCurSerCenter(admin);
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 查询某组下的话务员
	 * @return null
	 * @author liy
	 * @datetime 2014年12月17日,上午10:58:43
	 */
	@Action(value="sms_cur_ser")
	public String curSerView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		list = this.baseAllService.getSmsService().findCurSerByGroupId(callSms.getGroupId());
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 短信批量导入
	 * @return String
	 * @author liy
	 * @datetime 2015年2月13日,下午4:39:39
	 */
	@Action(value = "sms_import_save")
	public String importSave() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		map = new HashMap<String, Object>();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		try {
			File file = this.getImportFile(request);
			String info = this.baseAllService.getSmsService().importSmsExcel(admin, file);
			if(info.equals("true")) {
				map.put("success", true);
			} else {
				map.put("success", info);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 查询是否发送生日祝福短信
	 * @return null
	 * @author liy
	 * @datetime 2015年2月14日,下午3:25:23
	 */
	@Action(value = "sms_is_send")
	public String isSend() {
		HttpServletResponse response = ServletActionContext.getResponse();
		boolean flag = this.baseAllService.getSmsService().isSend();
		map = new HashMap<String, Object>();
		map.put("success", flag);
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 保存短信发送提醒设置
	 * @return null
	 * @author liy
	 * @datetime 2015年2月14日,下午3:40:42
	 */
	@Action(value = "sms_save_switch")
	public String saveSwitch() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		String keyValue = request.getParameter("keyValue");
		boolean flag = this.baseAllService.getSmsService().saveSwitch(keyValue);
		map = new HashMap<String, Object>();
		map.put("success", flag);
		formatJSON(response, map);
		return null;
	}
	
	@Override
	public MedicalCallSms getModel() {
		return callSms;
	}

}
